/*
 *  Copyright 2006 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.persistence.hibernate.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.event.DeleteEvent;
import org.hibernate.event.def.DefaultDeleteEventListener;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>Event listener that reacts on delete events. It first executes some
 * processors and then behaves as a default listener (i.e. proceedes with
 * detetion).
 * </p>
 * <p><a href="DeleteEntityEventListener.java.html"><i>View source</i></a></p>
 *
 * @author Roman Puchkovskiy <a href="mailto:roman.puchkovskiy@blandware.com">
 *         &lt;roman.puchkovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.1 $ $Date: 2006/10/03 11:22:57 $
 */
public class DeleteEntityEventListener extends DefaultDeleteEventListener {

    protected transient final Log log = LogFactory.getLog(DeleteEntityEventListener.class);

    /**
     * List of processors that will be executed for each event
     */
    protected List processors = new ArrayList();

    /**
     * Sets list of processors that will process each event.
     *
     * @param processors list of processors
     */
    public void setProcessors(List processors) {
        this.processors = processors;
    }

    /**
     * Called when delete event occures. Executes all processors and then
     * proceedes deletion.
     *
     * @param event event to process
     * @throws HibernateException
     */
    public void onDelete(DeleteEvent event) throws HibernateException {
        for (int i = 0; i < processors.size(); i++) {
            DeleteProcessor processor = (DeleteProcessor) processors.get(i);
            processor.processDelete(event);
        }

        super.onDelete(event);
    }
}
